$(function () {
    // if (editType != 'info') {
    //     $("#fristIconUrl").removeAttrs("required");
    //     $("#secondIconUrl").removeAttrs("required");
    // }
    if (editType != 'info') {
        regValidate(["#_bizEnumDataForm"]);
    }

    $("input[name=labelState]").click(function(){
        changeDiv();
    });

    var value = $("input[name=labelState]:checked").val();
    if(value == 'plain_text'){
        $("#textDiv").show();
        $("#linkDiv").hide();
    }else{
        $("#textDiv").hide();
        $("#linkDiv").show();
    }
});

function doBizEnumSave() {
    console.log($("#_bizEnumDataForm").valid());
    if(!$("#_bizEnumDataForm").valid()) return;//校验不通过

    var value = $("input[name=labelState]:checked").val();
    if(value == 'link'){
        var imageUrl = document.getElementById("imageUrl");
        if($.trim($("#labelImage").val()).length > 0 && (imageUrl.naturalWidth != 750 || imageUrl.naturalHeight != 180)){
            toastr.error("标签图片的尺寸不符合750*180");
            return;
        }
    }

    var fristIconUrlImg = document.getElementById("fristIconUrlImg");
    if($.trim($("#fristIconUrl").val()).length > 0 && (fristIconUrlImg.naturalWidth != 120 || fristIconUrlImg.naturalHeight != 120)){
        toastr.error("有效图片的尺寸不符合120*120");
        return;
    }
    var secondIconUrlImg = document.getElementById("secondIconUrlImg");
    if($.trim($("#secondIconUrl").val()).length > 0 &&  (secondIconUrlImg.naturalWidth != 120 || secondIconUrlImg.naturalHeight != 120)){
        toastr.error("无效图片的尺寸不符合120*120");
        return;
    }


    if(value == 'link') {
        var idList = ['labelImage', 'fristIconUrlfile', 'secondIconUrlFile'];
        var sizeList = [1024 * 60, 1024 * 60, 1024 * 60];
        var msgList = ["标签图片大小超出限制范围(60k以内)", "有效图片大小超出限制范围(60k以内)", "无效图片大小超出限制范围(60k以内)"];
        if (!filesSizeCheckWithForceFlag(idList, sizeList, msgList, null, true)) {
            return true;
        }
    }else{
        var idList = ['fristIconUrlfile', 'secondIconUrlFile'];
        var sizeList = [1024 * 60, 1024 * 60];
        var msgList = ["有效图片大小超出限制范围(60k以内)", "无效图片大小超出限制范围(60k以内)"];
        if (!filesSizeCheckWithForceFlag(idList, sizeList, msgList, null, true)) {
            return true;
        }
    }
    var postData ={};
    $("#_bizEnumDataForm").serializeArray().map((function (item,index,input) {
        postData[item.name] = item.value;
    }));
    bizEnumGroupMap.map((function (item,index,input) {
        if(item.code == postData.groupCode){
            postData.groupName = item.name;
        }
    }));
   // postData.groupName = bizEnumGroupMap[postData.groupCode];
   if(postData.sortNum == null || postData.sortNum == ''){
       postData.sortNum = 999;
   }
    var value = $("input[name=labelState]:checked").val();
   if(value == 'plain_text'){
       postData.labelInstruction = $("#instructionText").val();
   }else{
       postData.labelInstruction = $("#instructionUrl").val();
       postData.labelImageUrl = $("#labelImageUrl").val();
       if($("#labelImageUrl").val() == null || $("#labelImageUrl").val() == ''){
           var imageUrl = document.getElementById("imageUrl");
           postData.labelImageUrl = imageUrl.src;
       }

   }
    var url = basePath + 'bizEnum/addBizEnum?t=' + new Date().getTime();
    if (editType == 'edit') {
        url = basePath + 'bizEnum/editBizEnum?t=' + new Date().getTime();
    }
    $.ajax({
        type: 'post',
        url: url,
        dataType: 'json',
        data: postData,
        success : function (data) {
            if (!data) {
                toastr.error(common_save_fail);
                return;
            }
            if (data.isSuccess) {
                toastr.success(common_save_success);
                doBack();
                return;
            }
            if (!data.isSuccess && data.errorCode == 'EXIST_CODE') {
                toastr.error('【'+enumName+'】编码已存在');
                return;
            }
            if (!data.isSuccess) {
                toastr.error(data.errorDesc);
                return;
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
            return;
        }
    });
}

function doBack() {
    window.location.href = basePath + 'bizEnum/bizEnumPage?status=' + searchInfo.status + '&enumName=' + searchInfo.enumName+ '&bizCode=' + searchInfo.bizCode
        + '&groupCode=' + searchInfo.groupCode +'&t=' + new Date().getTime() + '&pageNo=' + searchInfo.pageNo + '&pageSize=' + searchInfo.pageSize
        + '&GpageNo=' + searchInfo.gpageNo + '&GpageSize=' + searchInfo.gpageSize;
}

function changeDiv(){
    var value = $("input[name=labelState]:checked").val();
    if(value == 'plain_text'){
        $("#textDiv").show();
        $("#linkDiv").hide();
    }else{
        $("#textDiv").hide();
        $("#linkDiv").show();
    }
}

function doCheckDataChange() {
    var data = getFromData('#_bizEnumDataForm');
    var checkData = {
        groupCode : data.groupCode,
        enumName : data.enumName,
        bizCode : data.bizCode,
        sortNum : data.sortNum,
        remark : data.remark,
    };
    var fristIconUrl =  $("#fristIconUrl").val();
    var secondIconUrl =  $("#secondIconUrl").val();
    if (editType != 'info' && ( (!Compare(checkData, oneBizEnumInfo)) || fristIconUrl.length > 0 ||  secondIconUrl.length > 0)) {
        swalClosePageToastr(doBack);
        return;
    }
    doBack();
}

// var freeMap = new HashMap();
function updateImageAndShow(e,showImageId,inputId) {
    var fileId = $(e).attr("id");
    var fileObj = document.getElementById(fileId);
    var file = fileObj.files[0];
    if(file != undefined){
        createReader(file, function (width, height) {
            var size = file.size / 1024;
            var value = $("input[name=labelState]:checked").val();
            if(value == 'link' && fileId == 'labelImage'){
                if(width != 750 || height != 180){
                    toastr.error("标签图片的尺寸不符合750*180");
                    return;
                }
                if(size > 60){
                    toastr.error("标签图片大小超出限制范围(60k以内)");
                    return ;
                }
            }

            if(fileId == 'fristIconUrlfile'){
                if(width != 120 || height != 120){
                    toastr.error("有效图片的尺寸不符合120*120");
                    return;
                }
                if(size > 60){
                    toastr.error("有效图片大小超出限制范围(60k以内)");
                    return;
                }
            }

            if(fileId == 'secondIconUrlFile'){
                if(width != 120 || height != 120){
                    toastr.error("无效图片的尺寸不符合120*120");
                    return;
                }
                if(size > 60){
                    toastr.error("无效图片大小超出限制范围(60k以内)");
                    return;
                }
            }
            showLoading();
            uploadFileToOssSync($(e).attr("id"),false,function (result) {
                // 这是直接放url
                $("#"+showImageId).attr("src",result.fileUrl);
                $("#"+inputId).val(result.fileUrl);
                // $("#"+showImageId).
                hideLoading();
            },function (e) {
                console.log("这是失败的回调");
                hideLoading();
            });
        });

    }
}


createReader = function(file, whenReady) {
    var reader = new FileReader;
    reader.onload = function (evt) {
        var image = new Image();
        image.onload = function () {
            var width = this.width;
            var height = this.height;
            if (whenReady) whenReady(width, height);
        };
        image.src = evt.target.result;
    };
    reader.readAsDataURL(file);
};
